package com.admin.service;

import com.admin.dao.HospitalizationDao;
import com.admin.entity.Hospitalization;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.utils.admin.ObjectUtils;
import com.admin.sys.base.module.extend.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

//住院信息接口类
@Service
public class HospitalizationService extends BaseService {

    @Autowired
    private HospitalizationDao hospitalizationDao;

    //【住院信息】设置查询条件
    private LambdaQueryWrapper<Hospitalization> getHospitalizationQueryCondition(Hospitalization hospitalization) {
        LambdaQueryWrapper<Hospitalization> lambdaQuery = this.getBaseQueryCondition(hospitalization);
        //根据创建时间排序
        lambdaQuery.orderByDesc(Hospitalization::getCreateDate);
        if (ObjectUtils.isNotNull(hospitalization)) {
            //【主键id】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(hospitalization.getId()), Hospitalization::getId, hospitalization.getId());
            //【床位号】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(hospitalization.getBedNo()), Hospitalization::getBedNo, hospitalization.getBedNo());
            //【患者姓名】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(hospitalization.getRegisterId()), Hospitalization::getRegisterId, hospitalization.getRegisterId());
            //【主治医生】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(hospitalization.getUserId()), Hospitalization::getUserId, hospitalization.getUserId());
            //【就诊科室】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(hospitalization.getOfficeId()), Hospitalization::getOfficeId, hospitalization.getOfficeId());
            //【入院时间】精确查询
            lambdaQuery.eq(ObjectUtils.isNotNull(hospitalization.getInTime()), Hospitalization::getInTime, hospitalization.getInTime());
            //【入院时间】范围查询
            if (ObjectUtils.isNotNull(hospitalization.getInTimeRange()) && hospitalization.getInTimeRange().size() > 0) {
                lambdaQuery.between(Hospitalization::getInTime, hospitalization.getInTimeRange().get(0), hospitalization.getInTimeRange().get(1));
            }
            //【出院时间】精确查询
            lambdaQuery.eq(ObjectUtils.isNotNull(hospitalization.getOutTime()), Hospitalization::getOutTime, hospitalization.getOutTime());
            //【出院时间】范围查询
            if (ObjectUtils.isNotNull(hospitalization.getOutTimeRange()) && hospitalization.getOutTimeRange().size() > 0) {
                lambdaQuery.between(Hospitalization::getOutTime, hospitalization.getOutTimeRange().get(0), hospitalization.getOutTimeRange().get(1));
            }
            //【住院费用】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(hospitalization.getFee()), Hospitalization::getFee, hospitalization.getFee());
        }
        return lambdaQuery;
    }

    //【住院信息】分页查询
    public IPage<Hospitalization> getPage(Page<Hospitalization> page, Hospitalization hospitalization) {
        LambdaQueryWrapper<Hospitalization> lambdaQuery = getHospitalizationQueryCondition(hospitalization);
        return hospitalizationDao.selectPage(page, lambdaQuery);
    }
    
    //【住院信息】查询列表
    public List<Hospitalization> getList(Hospitalization hospitalization) {
        LambdaQueryWrapper<Hospitalization> lambdaQuery = getHospitalizationQueryCondition(hospitalization);
        return hospitalizationDao.selectList(lambdaQuery);
    }
    
    //【住院信息】根据id查询
    public Hospitalization get(String id) {
        return hospitalizationDao.selectById(id);
    }

    //【住院信息】根据对象查询
    public Hospitalization get(Hospitalization hospitalization) {
    LambdaQueryWrapper<Hospitalization> lambdaQuery = getHospitalizationQueryCondition(hospitalization);
        return hospitalizationDao.selectOne(lambdaQuery);
    }
    
    //【住院信息】新增
    public int insert(Hospitalization hospitalization) {
        this.preInsert(hospitalization);
        return hospitalizationDao.insert(hospitalization);
    }
    
    //【住院信息】修改
    public int update(Hospitalization hospitalization) {
        this.preUpdate(hospitalization);
        return hospitalizationDao.updateById(hospitalization);
    }
    
    //【住院信息】删除
    public int delete(String id) {
        return hospitalizationDao.deleteById(id);
    }

    //【住院信息】批量删除
    public int delAll(String[] ids) {
        return hospitalizationDao.deleteBatchIds(Arrays.asList(ids));
    }

}